package com.songcha.library_base.mvvm.edit

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.bean.BaseEditBean
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel

abstract class BaseEditViewModel<RP: BaseRepository,ET:BaseEditBean>(app:Application):
    BaseViewModel<RP>(app) {
    val editList = MutableLiveData<MutableList<ET>>()
    val isManage = MutableLiveData(false)
    val isSelectAll = MutableLiveData(false)
    val hasSelect = MutableLiveData(false)

    protected open fun initEditList(list:MutableList<ET>){
        isSelectAll.value = false
        hasSelect.value = false
        isManage.value = false
        editList.value = list
    }


    protected open fun updateItemSelectValue(list:MutableList<ET>,index:Int,isSelect: Boolean){
        list[index].isSelect = isSelect
    }

    fun selectAll(){
        val tmp = getEditListValue()
        for(i in 0 until tmp.size){
            //tmp[i].isSelect = true
            //setItemIsSelect(i,true)
            updateItemSelectValue(tmp,i,true)
        }
        editList.value = tmp
        isSelectAll.value = true
    }

    fun unSelectAll(){
        val tmp = getEditListValue()
        for(i in 0 until tmp!!.size){
            //tmp[i].isSelect = false
            //setItemIsSelect(i,false)
            updateItemSelectValue(tmp,i,false)
        }
        editList.value = tmp
        isSelectAll.value = false
    }

    fun updateSelectAll(){
        if(editList.value == null) return
        var selectCount = 0
        for(i in 0 until editList.value!!.size){
            if(editList.value!![i].isSelect) selectCount++
        }
        isSelectAll.value = (selectCount == editList.value!!.size)
        hasSelect.value = selectCount > 0
    }

    fun setIsManage(isManage:Boolean){
        this.isManage.value = isManage
        if(!isManage) unSelectAll()
    }

    /*fun reverseIsManage(){
        isManage.value = !isManage.value!!
    }*/

    fun getSelectItemKeys(block:(ET)->Int):MutableList<Int>{
        val ids = mutableListOf<Int>()
        for(item in editList.value!!){
            if(item.isSelect){
                val id = block(item)
                if(id > 0)
                    ids.add(id)
            }
        }
        return ids
    }

    fun getSelectList():MutableList<ET>{
        val list = mutableListOf<ET>()
        for(item in editList.value!!){
            if(item.isSelect){
               list.add(item)
            }
        }
        return list
    }

    fun setItemIsSelect(index:Int,isSelect:Boolean){
        if(editList.value.isNullOrEmpty()) return
        if(index< 0 || index > editList.value!!.size - 1) return
        setItemIsSelectValue(index,isSelect)
        updateSelectAll()
    }

    fun reverseSelectItem(index:Int){
        if(editList.value.isNullOrEmpty()) return
        if(index< 0 || index > editList.value!!.size - 1) return
        val isSelect = editList.value!![index].isSelect
        setItemIsSelectValue(index,!isSelect)
        updateSelectAll()
    }

    protected open fun getEditListValue():MutableList<ET>{
        return editList.value!!
    }

    protected open fun setItemIsSelectValue(index:Int,isSelect:Boolean){
        editList.value!![index].isSelect = isSelect
    }


}